package com.rr.study02.venda;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

public class VendaDaoJpa implements VendaDao{

	private EntityManager entManager;
	
	public EntityManager getEntManager() {
		return entManager;
	}

	public void setEntManager(EntityManager entManager) {
		this.entManager = entManager;
	}

	@Override
	public List<Venda> listar() {
		CriteriaBuilder criBuilder = entManager.getCriteriaBuilder();
		CriteriaQuery<Venda> criQuery = criBuilder.createQuery(Venda.class);
		Root<Venda> vendaRoot = criQuery.from(Venda.class);
		criQuery.select(vendaRoot);
		return entManager.createQuery(criQuery).getResultList();
	}

	@Override
	public void registrarVenda(Venda venda) {
		entManager.persist(venda);	
	}

	@Override
	public void excluir(Venda venda) {
		entManager.remove(venda);
	}
	
}
